package com.samp.bblock.core.security;

import com.samp.bblock.util.AesCbcUtil;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;


/**
 * 对加密项进行解密
 *
 *
 */
public class SecurityPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    /**
     * 加密密钥
     */
    private String encryptKey = "Dq!w4tg&ty90pI6G";
    
    /**
     * 重写父类方法，解密指定属性名对应的属性值
     */
    @Override
    protected String convertProperty(String propertyName,String propertyValue){
        if(isEncryptPropertyVal(propertyName)){
            return AesCbcUtil.secretKey16Decrypt(propertyValue, encryptKey);
        }else{
            return propertyValue;
        }
    }
    /**
     * 判断属性值是否需要解密，这里我约定需要解密的属性名用encrypt开头
     * @param propertyName
     * @return
     */
    private boolean isEncryptPropertyVal(String propertyName){
        if(propertyName.startsWith("encrypt")){
            return true;
        }else{
            return false;
        }
    }
    
    public static void main(String[] args) {
    	SecurityPropertyPlaceholderConfigurer a = new SecurityPropertyPlaceholderConfigurer();
    	String dd = a.convertProperty("encrypt:password", "d4f97440d8d74e5c75355c813dd0a74e");
    	System.out.println(dd);
	}
}
